package Service.Implement;

import Dao.Implement.adminDaoImplement;
import Dao.adminDao;
import Service.adminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import pojo.admin;
import pojo.playground;
import pojo.room;
import pojo.stadium;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service(value = "adminServiceImplement")
public class adminServiceImplement implements adminService {
    @Autowired
    private adminDao adminDao;

    @Override
    public admin queryAdminByID(String adminID) {
        return adminDao.queryById(adminID);
    }

    @Override
    public boolean loginIn(String vxId, String password) {
      return adminDao.testIdPwd(vxId,password);
    }

    @Override
    public int register(String vxId,String name, String password) {

        return adminDao.register(vxId,name,password);
    }

    @Override
    public List<stadium> queryStadiumBYName(String stadiumName) {
        return adminDao.queryStadiumByName( stadiumName);
    }

    @Override
    public List<stadium> queryForStadiums(String vxId) {
        return adminDao.queryStadium(vxId);
    }

    @Override
    public List<playground> queryForPlaygrounds(Long stadiumID) {
        return adminDao.queryPlaygroundListByStadium(stadiumID);
    }

    @Override
    public List<room> queryRoomByDate(String date) {
        return adminDao.queryRoomByDate(date);
    }

    @Override
    public List<room> queryRoom(String id) {
        return adminDao.queryRoom(id);
    }

    @Override
    public List<room> queryRoomByRoomState(int roomstate,String Id) {
        return adminDao.queryRoomByRoomState(roomstate,Id);
    }

    @Override
    public List<room> queryRoomByStadiumName(String stadiumName) {
        return adminDao.queryRoomByStadiumName(stadiumName);
    }

    @Override
    public room queryRoomById(Long roomId) {
        return adminDao.queryRoomById(roomId);
    }

    @Override
    public int addStadium(String managerId, String stadiumName, String location, int opentime, int closetime, String sportsId, String category,String display) {
        stadium stadium=new stadium();
        stadium.setSports_id(sportsId);
        stadium.setManager(managerId);
        stadium.setClosetime(closetime);
        stadium.setCategory(category);
        stadium.setOpentime(opentime);
        stadium.setStadiumDisplay(display);
        stadium.setName(stadiumName);
        stadium.setLocation(location);
        return adminDao.addStadium(stadium);
    }

    @Override
    public int deleteStadium(Long stadiumId) {
        return adminDao.deleteStadium(stadiumId);
    }


    @Override
    public int deletePlayground(Long playgroundId, Long stadiumId) {
        return adminDao.deletePlayground(playgroundId,stadiumId);
    }

    @Override
    public int updateStadium(Long stadium_id, String managerId, String stadiumName, String location, int opentime, int closetime, String sportsId, String category, String display) {
        stadium stadium=adminDao.queryStadiumById(stadium_id);
        stadium.setManager(managerId);
        stadium.setName(stadiumName);
        stadium.setLocation(location);
        stadium.setOpentime(opentime);
        stadium.setClosetime(closetime);
        stadium.setCategory(category);
        stadium.setSports_id(sportsId);
        stadium.setStadiumDisplay(display);
        return adminDao.updateStadium(stadium);
    }


    @Override
    public int addPlayground(String playgroundName, String sport_id, float price, String display, Long stadiumId) {
        playground playground=new playground();
        int opentime=adminDao.queryStadiumById(stadiumId).getOpentime();
        int closetime=adminDao.queryStadiumById(stadiumId).getClosetime();
        playground.setRealTime(opentime,closetime,"today");
        playground.setRealTime(opentime,closetime,"tomorrow");
        playground.setPlayground_display(display);
        playground.setPlay_name(playgroundName);
        playground.setPrice(price);
        playground.setSport_id(sport_id);
        playground.setStadium_id(stadiumId);
        return adminDao.addPlayground(playground);
    }

    @Override
    public int updatePlayground(String display, String playgroundName, float price, String sport_id, Long playgroundId) {
        playground playground=adminDao.queryPlaygroundById(playgroundId);
        playground.setPlayground_display(display);
        playground.setPrice(price);
        playground.setPlay_name(playgroundName);
        playground.setSport_id(sport_id);
        return adminDao.updatePlaygroundState(playground);
    }


    @Override
    public List<room> queryRoomByPeriod(String startTime,String Id) {
        java.util.Date endDate= new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date startDate=null;
        try {
            startDate= sdf.parse(startTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

         Date startTimeUse=new Date(startDate.getTime());
        Date endTimeUse=new Date(endDate.getTime());
        System.out.println( startTimeUse+" "+endTimeUse);
        return adminDao.queryRoomOrderTap(startTimeUse,endTimeUse,Id);

    }
}
